/***************************************************************************
 * 版权所有：深圳市一非科技有限公司
 * 文件名称：HandBlueValue.m
 * 内容摘要：手环蓝牙原始数据类
 * 编辑作者：彭于
 * 版本编号：1.0.1
 * 创建日期：2016年11月15日
 ***************************************************************************/

#import "HandBlueValue.h"
#import "HandBlueMethod.h"

HandBlueValue *g_handBlueValue = nil;

@implementation HandBlueValue
{
    NSMutableArray      *m_heartRateArray;
    NSMutableDictionary *m_heartRateDic;
    int checkLeiJiNum;
}

/***************************************************************************
 * 方法名称：init
 * 功能描述：初始化
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (id)init
{
    checkLeiJiNum = 0;
    m_heartRateDic = [[NSMutableDictionary alloc] init];
    m_heartRateArray = [[NSMutableArray alloc] init];
    
    if (g_handBlueValue != nil)
    {
        return nil;
    }
    
    self = [super init];
    return self;
}

/***************************************************************************
 * 方法名称：selfAlloc
 * 功能描述：单例化
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
+ (HandBlueValue *)selfAlloc
{
    if (g_handBlueValue == nil)
    {
        g_handBlueValue = [[HandBlueValue alloc] init];
    }
    
    return g_handBlueValue;
}

/***************************************************************************
 * 方法名称：handBluetoothValue
 * 功能描述：原始数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (void)handBluetoothValue:(NSData *)value
{
    NSString *hexString  = [NSString stringWithFormat:@"%@",value];
    hexString = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
    hexString = [hexString stringByReplacingOccurrencesOfString:@"<" withString:@""];
    hexString = [hexString stringByReplacingOccurrencesOfString:@">" withString:@""];
    [self stringBlueValue:hexString];
}

/***************************************************************************
 * 方法名称：endBluetoothValue
 * 功能描述：结束数据
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (NSArray *)endBluetoothValue
{
    HandBlueMethod *handMethod = [[HandBlueMethod alloc] init];
    
    NSMutableArray *heartRateData = [[NSMutableArray alloc] initWithArray:[handMethod getXinLvResultData:m_heartRateArray]];
    
    checkLeiJiNum = 0;
    [m_heartRateDic removeAllObjects];
    [m_heartRateArray removeAllObjects];
    
    return heartRateData;
}

/***************************************************************************
 * 方法名称：selfAlloc
 * 功能描述：单例化
 * 输入参数：
 * 返回数据：
 ***************************************************************************/
- (void)stringBlueValue:(NSString *)myString
{
    if (myString.length < 4)
    {
        return;
    }
    
    int markNum = [[[myString substringFromIndex:1] substringToIndex:1] intValue];
    
    if ([[[myString substringFromIndex:1] substringToIndex:1] isEqualToString:@"a"] || [[[myString substringFromIndex:1] substringToIndex:1] isEqualToString:@"A"])
    {
        markNum = 10;
    }
    
    int markLongNum = [[[myString substringFromIndex:3] substringToIndex:1] intValue];
    
    int j=0;
    Byte bytes[20];
    
    for(int i=0;i<[myString length];i++)
    {
        int int_ch; //// 两位16进制数转化后的10进制数
        
        unichar hex_char1 = [myString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16; //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            int_ch1 = (hex_char1-87)*16; //// a 的Ascll - 97
        i++;
        
        unichar hex_char2 = [myString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            int_ch2 = hex_char2-87; //// a 的Ascll - 97
        
        int_ch = int_ch1+int_ch2;
        //        NSLog(@"int_ch=%d",int_ch);
        bytes[j] = int_ch; ///将转化后的数放入Byte数组里
        j++;
    }
    
    int byteLong = (int)myString.length/2;
    int beginNum = 0;
    int endNum  = bytes[byteLong-1]&0xFF;
    for (int i = 0; i < byteLong-1; i++)
    {
        int test = bytes[i]&0xFF;
        beginNum = beginNum+test;
    }
    
    beginNum = beginNum%256;
    
    if (endNum != beginNum)
    {
        NSLog(@"*****************************检验和不一致*****************************");
        return;
    }
    
    if (markNum == 1)
    {
        
    }
    else if (markNum == 2)
    {
        int c1 = bytes[1]&0xFF;
        int c2 = bytes[2]&0xFF;
        int c3 = bytes[3]&0xFF;
        int c4 = bytes[4]&0xFF;
        NSString *timeMinutes = [NSString stringWithFormat:@"%d",c4+(c3<<8)+(c2<<16)+(c1<<24)];
        NSDate *handTime = [NSDate dateWithTimeIntervalSince1970:[timeMinutes longLongValue]];
        NSLog(@"手环时间为：%@",handTime);
    }
    else if (markNum == 3)
    {
        
    }
    else if (markNum == 4)
    {
        
    }
    else if (markNum == 5)
    {
        
    }
    else if (markNum == 6)
    {
        return;
    }
    else if (markNum == 7)
    {
        
    }
    else if (markNum == 8)
    {
        if (markLongNum == 1)
        {
            if (m_heartRateDic.allKeys.count > 0)
            {
                NSDictionary *dic1 = [[NSDictionary alloc] initWithDictionary:m_heartRateDic];
                [m_heartRateArray addObject:dic1];
                [m_heartRateDic removeAllObjects];
            }
            
            [m_heartRateDic setObject:myString forKey:@"xinLv1"];
            checkLeiJiNum ++;
        }
        else if (markLongNum == 2)
        {
            [m_heartRateDic setObject:myString forKey:@"xinLv2"];
            checkLeiJiNum ++;
        }
        else if (markLongNum == 3)
        {
            [m_heartRateDic setObject:myString forKey:@"xinLv3"];
            checkLeiJiNum ++;
        }
        else if (markLongNum == 4)
        {
            [m_heartRateDic setObject:myString forKey:@"xinLv4"];
            checkLeiJiNum ++;
        }
        else if (markLongNum == 5)
        {
            int c2 = bytes[2]&0xFF;
            
            if (checkLeiJiNum != c2)
            {
                checkLeiJiNum = 0;
                [m_heartRateDic removeAllObjects];
                return;
            }
            else
            {
                NSDictionary *dic1 = [[NSDictionary alloc] initWithDictionary:m_heartRateDic];
                [m_heartRateArray addObject:dic1];
                [m_heartRateDic removeAllObjects];
                checkLeiJiNum = 0;
            }
        }
    }
    else if (markNum == 9)
    {
        
    }
    else if (markNum == 10)
    {
        
    }
}


@end
